<?php

/**
 * Skola form base class.
 *
 * @package    dnevnik
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfPropelFormGeneratedTemplate.php 16976 2009-04-04 12:47:44Z fabien $
 */
class BaseSkolaForm extends BaseFormPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                  => new sfWidgetFormInputHidden(),
      'tip'                 => new sfWidgetFormInput(),
      'skola_id'            => new sfWidgetFormPropelChoice(array('model' => 'Skola', 'add_empty' => true)),
      'ime'                 => new sfWidgetFormInput(),
      'okrug_id'            => new sfWidgetFormPropelChoice(array('model' => 'Okrug', 'add_empty' => false)),
      'mesto'               => new sfWidgetFormInput(),
      'adresa'              => new sfWidgetFormInput(),
      'opstina'             => new sfWidgetFormInput(),
      'telefon'             => new sfWidgetFormInput(),
      'fax'                 => new sfWidgetFormInput(),
      'web'                 => new sfWidgetFormInput(),
      'maticni_broj'        => new sfWidgetFormInput(),
      'sifra_skole'         => new sfWidgetFormInput(),
      'klasa'               => new sfWidgetFormInput(),
      'resenje_br'          => new sfWidgetFormInput(),
      'resenje_dat'         => new sfWidgetFormInput(),
      'delovodni_br'        => new sfWidgetFormInput(),
      'resenje_organ'       => new sfWidgetFormInput(),
      'clanarina'           => new sfWidgetFormInput(),
      'tip_obrasca'         => new sfWidgetFormInput(),
      'created_at'          => new sfWidgetFormDateTime(),
      'menadzer_skola_list' => new sfWidgetFormPropelChoiceMany(array('model' => 'Menadzer')),
      'upravnik_skola_list' => new sfWidgetFormPropelChoiceMany(array('model' => 'Upravnik')),
    ));

    $this->setValidators(array(
      'id'                  => new sfValidatorPropelChoice(array('model' => 'Skola', 'column' => 'id', 'required' => false)),
      'tip'                 => new sfValidatorInteger(),
      'skola_id'            => new sfValidatorPropelChoice(array('model' => 'Skola', 'column' => 'id', 'required' => false)),
      'ime'                 => new sfValidatorString(array('max_length' => 45)),
      'okrug_id'            => new sfValidatorPropelChoice(array('model' => 'Okrug', 'column' => 'id')),
      'mesto'               => new sfValidatorString(array('max_length' => 30)),
      'adresa'              => new sfValidatorString(array('max_length' => 60)),
      'opstina'             => new sfValidatorString(array('max_length' => 45, 'required' => false)),
      'telefon'             => new sfValidatorString(array('max_length' => 15, 'required' => false)),
      'fax'                 => new sfValidatorString(array('max_length' => 15, 'required' => false)),
      'web'                 => new sfValidatorString(array('max_length' => 45, 'required' => false)),
      'maticni_broj'        => new sfValidatorString(array('max_length' => 15, 'required' => false)),
      'sifra_skole'         => new sfValidatorString(array('max_length' => 15, 'required' => false)),
      'klasa'               => new sfValidatorString(array('max_length' => 15, 'required' => false)),
      'resenje_br'          => new sfValidatorString(array('max_length' => 15, 'required' => false)),
      'resenje_dat'         => new sfValidatorString(array('max_length' => 15, 'required' => false)),
      'delovodni_br'        => new sfValidatorString(array('max_length' => 30, 'required' => false)),
      'resenje_organ'       => new sfValidatorString(array('max_length' => 80, 'required' => false)),
      'clanarina'           => new sfValidatorInteger(),
      'tip_obrasca'         => new sfValidatorInteger(),
      'created_at'          => new sfValidatorDateTime(array('required' => false)),
      'menadzer_skola_list' => new sfValidatorPropelChoiceMany(array('model' => 'Menadzer', 'required' => false)),
      'upravnik_skola_list' => new sfValidatorPropelChoiceMany(array('model' => 'Upravnik', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('skola[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    parent::setup();
  }

  public function getModelName()
  {
    return 'Skola';
  }


  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['menadzer_skola_list']))
    {
      $values = array();
      foreach ($this->object->getMenadzerSkolas() as $obj)
      {
        $values[] = $obj->getMenadzerId();
      }

      $this->setDefault('menadzer_skola_list', $values);
    }

    if (isset($this->widgetSchema['upravnik_skola_list']))
    {
      $values = array();
      foreach ($this->object->getUpravnikSkolas() as $obj)
      {
        $values[] = $obj->getUpravnikId();
      }

      $this->setDefault('upravnik_skola_list', $values);
    }

  }

  protected function doSave($con = null)
  {
    parent::doSave($con);

    $this->saveMenadzerSkolaList($con);
    $this->saveUpravnikSkolaList($con);
  }

  public function saveMenadzerSkolaList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['menadzer_skola_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(MenadzerSkolaPeer::SKOLA_ID, $this->object->getPrimaryKey());
    MenadzerSkolaPeer::doDelete($c, $con);

    $values = $this->getValue('menadzer_skola_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new MenadzerSkola();
        $obj->setSkolaId($this->object->getPrimaryKey());
        $obj->setMenadzerId($value);
        $obj->save();
      }
    }
  }

  public function saveUpravnikSkolaList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['upravnik_skola_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(UpravnikSkolaPeer::SKOLA_ID, $this->object->getPrimaryKey());
    UpravnikSkolaPeer::doDelete($c, $con);

    $values = $this->getValue('upravnik_skola_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new UpravnikSkola();
        $obj->setSkolaId($this->object->getPrimaryKey());
        $obj->setUpravnikId($value);
        $obj->save();
      }
    }
  }

}
